package com.atguigu.myjuc.interactive;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

// V1 synchronized + wait + notifyAll 传统老版本
//资源类OOP = Field + Method
//-----------------------------------synchronized和wait，notifyAlly一起使用---------------------------------------
class AirCondition{
    //private int number = 0;
 /*   //两个方法，加减
    public  synchronized void increment() throws InterruptedException{
        //判断
        while (number != 0){
            this.wait();
        }
        //干活
        System.out.println(Thread.currentThread().getName()+"\t"+(++number));
        //通知
        this.notifyAll();
    }
    public  synchronized void decrement() throws InterruptedException{
        //判断
        while (number == 0){
            this.wait();
        }
        //干活
        System.out.println(Thread.currentThread().getName()+"\t"+(--number));
        //通知
        this.notifyAll();
    }*/
/**
 * 第二版v2 JUC新版，Lock + condition.await() + condition.signalAll()
 */
    private int number = 0;

    final Lock lock = new ReentrantLock();
    final Condition condition  = lock.newCondition();


    public void increment() throws InterruptedException{
       lock.lock();
        try {
            //判断
            while (number != 0){
                condition.await();//ynchronized和wait，notifyAlly一起使用所以这里不能用
            }
            //干活
            System.out.println(Thread.currentThread().getName()+"\t"+(++number));
            //通知
            condition.signalAll();
        }finally {
            lock.unlock();
        }
    }
    public void decrement() throws InterruptedException {
        lock.lock();
        try {
            //判断
            while (number == 0){
                condition.await();
            }
            //干活
            System.out.println(Thread.currentThread().getName()+"\t"+(--number));
            //通知
            condition.signalAll();
        }finally {
            lock.unlock();
        }
    }

}




public class ProdConsumerDemo {
    public static void main(String[] args) {
        AirCondition airCondition = new AirCondition();
        new Thread(()->{
            try {
                for (int i = 0; i < 10; i++) {
                    airCondition.increment();//
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        },"A").start();
        new Thread(()->{
            try {
                for (int i = 0; i < 10; i++) {
                    airCondition.decrement();
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        },"B").start();

        new Thread(()->{
            try {
                for (int i = 0; i < 10; i++) {
                    airCondition.increment();
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        },"C").start();
        new Thread(()->{
            try {
                for (int i = 0; i < 10; i++) {
                    airCondition.decrement();
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        },"D").start();
    }
}
